package com.hl.web;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.hl.pojo.Consultants;
import com.hl.service.ZixunshiPageService;
import com.hl.util.GeneralResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/zixunshipage")
@CrossOrigin
public class ZixunshiPage {
    File toFile=null;
    String path=null;
    @Autowired
    private ZixunshiPageService zixunshiPageService;
    @RequestMapping("/zixunshipages")
    public GeneralResult zixunshiPages(){
        System.out.println("------------");

        return GeneralResult.success(zixunshiPageService.consultants());

    }
    @RequestMapping("/zixunshi")
    public GeneralResult zixunshi(@RequestBody Map<String, Integer> map){
        Integer id = map.get("consultantId");
        System.out.println(id);

        List<Consultants> list = zixunshiPageService.zixunshi(id);
        System.out.println(list);
        return GeneralResult.success(list);
    }
    @RequestMapping("/shanchanglingyu")
    public GeneralResult shanchanglingyu(@RequestBody Map<String, Integer> map){
        Integer id = map.get("consultantId");
        System.out.println(id);
        return GeneralResult.success(zixunshiPageService.shanchanglingyu(id));
    }
    @RequestMapping("/liuyanhuifu")
    public GeneralResult liuyanhuifu(@RequestBody Map<String, Integer> map){
        Integer id = map.get("consultantId");
        System.out.println(id);
//        System.out.println(zixunshiPageService.liuyanhuifu(id));
        return GeneralResult.success(zixunshiPageService.liuyanhuifu(id));
    }
    @RequestMapping("/yonghupingjia")
    public GeneralResult yonghupingjia(@RequestBody Map<String, Integer> map){
        Integer id = map.get("consultantId");
        System.out.println(id);
//        System.out.println(zixunshiPageService.liuyanhuifu(id));
        return GeneralResult.success(zixunshiPageService.yonghupingjia(id));
    }
    @RequestMapping("/fabukecheng")
    public GeneralResult fabukecheng(@RequestBody Map<String, Integer> map){
        Integer id = map.get("consultantId");
        System.out.println(id);
//        System.out.println(zixunshiPageService.liuyanhuifu(id));
        return GeneralResult.success(zixunshiPageService.fabukecheng(id));
    }

    //vue 咨询师信息
    @RequestMapping("/consultantMessage")
    public GeneralResult getconsultantMessage(Integer pageNum,Integer pageSize){
        return GeneralResult.success(zixunshiPageService.getconsultantMessage(pageNum,pageSize));
    }
    @RequestMapping("/consultantMessageByName")
    public GeneralResult consultantMessageByName(@RequestBody Map<String, String> map){
        String consultantName = map.get("consultantName");
        String goodName = map.get("areaName");
        return GeneralResult.success(zixunshiPageService.getconsultantMessageByName(consultantName,goodName));
    }
    @RequestMapping("/getConsultantLevelList")
    public GeneralResult getConsultantLevelList(){
        return GeneralResult.success(zixunshiPageService.getConsultantLevelList());
    }
//    @RequestMapping("/upload")
//    public GeneralResult upload(MultipartFile file) throws IOException {
//        toFile = new File("E:\\十方智育\\sfzy-vue\\vue\\sfzy\\public\\images\\"+file.getOriginalFilename());
//        file.transferTo(toFile);
//        path="/images/"+file.getOriginalFilename();
//        System.out.println(toFile.getAbsolutePath());
//        return GeneralResult.success();
//    }
//@RequestMapping("/upload")
//@ResponseBody
//@CrossOrigin
//public GeneralResult upload(MultipartFile file) throws IOException {
//    //https://polaris-image.oss-cn-wuhan-lr.aliyuncs.com/cat1.jpg
//    //endPoint: oss-cn-wuhan-lr.aliyuncs.com 地域:北京
//    String endPoint = "oss-cn-beijing.aliyuncs.com";
//    //用户信息
//    String accessKeyId = "LTAI5tH6Xz2XWG8VUw2KcW4X";
//    String accessKeySecret = "Jyep2u0zLQRlLvTYodYDmM7qdoBec8";
//    //存储空间名称
//    String bucketName = "gaoqiping";
//    // 创建OSSClient实例。
//    OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
//    // 上传文件流。
//    InputStream inputStream = file.getInputStream();
//    //文件原名
//    String fileName = file.getOriginalFilename();
//    //生成随机文件名
//    fileName = UUID.randomUUID().toString().replace("-", "") + fileName;
//    //上传文件流
//    ossClient.putObject(bucketName, fileName, inputStream);
//    //关闭OSSClient。
//    ossClient.shutdown();
//    //返回文件路径
//    path="https://" + bucketName + "." + endPoint + "/" + fileName;
//    return GeneralResult.success();
//}
    @RequestMapping("/zixunshiAdd")
    public GeneralResult zixunshiAdd(@RequestBody Map<String, String> map){
        String consultantName = map.get("consultantName");
        String areaName = map.get("areaName");
        String region = map.get("region");
        String levelName = map.get("levelName");
        String consultantIntroduction = map.get("consultantIntroduction");
        String path = map.get("path");
        int num =zixunshiPageService.zixunshiAdd(consultantName,areaName,region,levelName,consultantIntroduction,path);
        if (num>0){
            return GeneralResult.success("添加成功");
        }else {
            return GeneralResult.fail();
        }
    }
    @RequestMapping("/zixunshiDelete")
    public GeneralResult zixunshiDelete(@RequestBody Map<String, Integer> map){
        Integer consultantId = map.get("consultantId");
        int num =zixunshiPageService.zixunshiDelete(consultantId);
        if (num>0){
            return GeneralResult.success("删除成功");
        }else {
            return GeneralResult.fail();
        }
    }
    @RequestMapping("/zixunshiUpdate")
    public GeneralResult zixunshiUpdate(@RequestBody Map<String,String> map){
        Integer consultantId = Integer.valueOf(map.get("consultantId"));
        String consultantName = map.get("consultantName");
        String areaName = map.get("areaName");
        String region = map.get("region");
        String levelName = map.get("levelName");
        String consultantIntroduction = map.get("consultantIntroduction");
        String path = map.get("path");
        System.out.println("*************************"+areaName);
        int num =zixunshiPageService.zixunshiUpdate(consultantId,consultantName,areaName,region,levelName,consultantIntroduction,path);
        if (num>0){
            return GeneralResult.success("修改成功");
        }else {
            return GeneralResult.fail();
        }
    }
}
